package com.gyg.bro.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * AESUtils
 *
 * @author
 * @date
 */
@Slf4j
public class AESUtils {

    /**
     * 加密算法
     */
    private static final String KEY_ALGORITHMS = "AES";
    /**
     * 字符编码
     */
    private static final String CHAR_SETS = "UTF-8";
    /**
     * AES的密钥长度
     */
    private static final Integer SECRET_KEY_LENGTHS = 128;
    /**
     * 加解密算法/工作模式/填充方式
     */
    private static final String DEFAULT_CIPHER_ALGORITHMS = "AES/ECB/PKCS5Padding";

    /**
     *
     * @param password
     * @return
     * @throws NoSuchAlgorithmException
     */
    private static SecretKeySpec getSecretKey(final String password) throws NoSuchAlgorithmException {
        //生成指定算法密钥的生成器
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHMS);
        keyGenerator.init(SECRET_KEY_LENGTHS, new SecureRandom(password.getBytes()));
        //生成密钥
        SecretKey secretKey = keyGenerator.generateKey();
        //转换成AES的密钥
        return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHMS);
    }

    /**
     * 加密
     * @param sSrc 待加密字符串
     * @param privateKey 私钥
     * @return
     */
    public static String aesEncrypt(String sSrc,String privateKey) {
        if(sSrc==null||"".equals(sSrc)){
            return null;
        }
        byte[] encrypted = new byte[0];
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            byte[] raw = privateKey.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return Base64.encodeBase64String(encrypted);
    }

    /**
     * 解密
     * @param sSrc 待解密字符串
     * @param privateKey 私钥
     * @return
     */
    public static String aesDecrypt(String sSrc,String privateKey) {
        if(sSrc==null||"".equals(sSrc)){
            return null;
        }
        try {
            byte[] raw = privateKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted = Base64.decodeBase64(sSrc.getBytes("UTF-8"));
            byte[] original = cipher.doFinal(encrypted);
            String originalString = new String(original, "utf-8");
            return originalString;
        } catch (Exception ex) {
            log.error("解密失败，sSrc={}", sSrc);
            ex.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {


        String r = AESUtils.aesDecrypt("j1+DsLWFBGwZEAum7yseKGFZebue2yOXXIN5hJ7Bb1I=", "aytofnapklzwyjuz");
        System.out.println(r);
    }

//    public static void main(String[] args) {
//        String s = "fNFkadfsJThpRMF5BGw0l3sMZoU93OVUObNtjcDibs8xKIrmuQ9+DJOSdROm3QdstdjyY8N9F18pi6dvEKxkcw==";
//        String r = AESUtils.aesDecrypt(s, "graphichdl85521566asekey32zyblfy");
//        System.out.println(r);
//    }

}